package com.example.games.mygame;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.example.games.core.Animation;
import com.example.games.core.CSprite;
import com.example.games.core.GameHelper;
import com.example.games.core.ScreenUtil;

public class Enemy implements CSprite {
	public final static int ANIM_COUNT = 4;
	private int sx, sy;
	private Bitmap spriteBmp;
	// 帧动画
	Animation mEnemyAnim;

	int circleX, circleY;

	double circleR;

	public Enemy(Bitmap bmp, int x, int y) {
		spriteBmp = bmp;
		sx = x;
		sy = y;

		Bitmap[] animBmp = new Bitmap[ANIM_COUNT];
		int cellW = bmp.getWidth() / ANIM_COUNT;
		int cellH = bmp.getHeight() / ANIM_COUNT;
		for (int i = 0; i < ANIM_COUNT; i++) {
			animBmp[i] = GameHelper.clipBitmap(bmp, i * cellW, cellH, cellW,
					cellH);
		}

		mEnemyAnim = new Animation(GameHelper.getContext(), animBmp, true);

	}

	@Override
	public void drawSelf(Canvas canvas, Paint paint) {
		// TODO Auto-generated method stub
		// canvas.drawBitmap(spriteBmp, ScreenUtil.getScreenX(sx),
		// ScreenUtil.getScreenY(sy), paint);
		if (isDead) {
			paint.reset();
			paint.setAlpha(0);
		}
		mEnemyAnim.DrawAnimation(canvas, paint, ScreenUtil.getScreenX(sx),
				ScreenUtil.getScreenY(sy));
	}

	@Override
	public void logic() {
		// TODO Auto-generated method stub
		sx -= 5;
	}

	@Override
	public int getX() {
		// TODO Auto-generated method stub
		return sx;
	}

	@Override
	public int getY() {
		// TODO Auto-generated method stub
		return sy;
	}

	@Override
	public int getSpriteWidth() {
		// TODO Auto-generated method stub
		return spriteBmp.getWidth();
	}

	@Override
	public int getSpriteHeight() {
		// TODO Auto-generated method stub
		return spriteBmp.getHeight();
	}

	@Override
	public boolean isTouched(float x, float y) {
		if (x >= sx && x <= (sx + getSpriteWidth()) && y >= sy
				&& y <= (sy + getSpriteHeight())) {
			return true;
		}
		return false;
	}

	@Override
	public int getCircleX() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int getCircleY() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public double getCircleR() {
		// TODO Auto-generated method stub
		return Math.sqrt(Math.pow(spriteBmp.getWidth() / 2, 2)
				+ Math.pow(spriteBmp.getHeight() / 2, 2));
	}

	@Override
	public int getType() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isCollidedEnemy(CSprite enemy) {
		// TODO Auto-generated method stub
		return false;
	}

	private int hp = 1000;

	private boolean isDead = false;

	@Override
	public void lostHp(int damage) {
		// TODO Auto-generated method stub
		hp -= damage;
		if (hp <= 0) {
			isDead = true;
		}
	}

	@Override
	public int getHp() {
		// TODO Auto-generated method stub
		return hp;
	}

	@Override
	public boolean isDead() {
		// TODO Auto-generated method stub
		return isDead;
	}

}
